import { EventMessage, EventSource } from 'src/app/services/event.service';
import { MatDialog } from '@angular/material/dialog';
import { Component, OnInit, OnChanges, SimpleChanges } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';
import {RequestReplyService} from '../../services/request-reply.service';
import {EventService} from '../../services/event.service';
import { RegisterDialogComponent } from "src/app/dialog/register-dialog/register-dialog.component";
import { SafeResourceUrl, DomSanitizer } from '@angular/platform-browser';
import { NGXLogger } from 'ngx-logger';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit, OnChanges {

  loginIdControl = new FormControl(null, Validators.required);

  passwordControl = new FormControl(null, Validators.required);


  isLogin = false;

  msg = '';

  iframe: SafeResourceUrl;

  constructor(
    private log: NGXLogger,
    private sanitizer: DomSanitizer,
    public dialog: MatDialog,
    private requestService: RequestReplyService,
    private eventService: EventService
  ) {
    // this.loginIdControl.setValue('30260855045')
    // this.passwordControl.setValue('123456')
    this.loginIdControl.valueChanges
      .subscribe(f => {
        this.clear();
      });

    this.passwordControl.valueChanges.subscribe(f => {
      this.clear();
    });

    // const src = this.requestService.GetBaseUrl() + '/tt/login/banner.jsp';
    // this.iframe = this.sanitizer.bypassSecurityTrustResourceUrl(src);
    // this.doRegister();
  }

  ngOnInit() {
    this.eventService.subscribeEvent((evt: EventMessage) => {
      switch (evt.source) {
        case EventSource.REGISTER_EVENT: {
          const tw: {phone: string, password: string } = evt.data;
          this.loginIdControl.setValue(tw.phone);
          this.passwordControl.setValue(tw.password);

          this.submit();

          break;
        }
      }});
  }

  submit() {
    if (this.isInvalid()) {
      return;
    }

    this.msg = '登录中';
    this.isLogin = true;

    const loginId = this.loginIdControl.value;
    const password = this.passwordControl.value;

    this.requestService.GetAccessToken({loginId, password})
      .subscribe((reply) => {
        this.isLogin = false;

        this.msg = reply.msg;
        if (reply.success) {
          this.refresh();
        }
      });
  }

  isInvalid() {
    return this.loginIdControl.invalid || this.passwordControl.invalid;
  }

  ngOnChanges(changes: SimpleChanges): void {

  }

  clear() {
    this.msg = null;
  }

  refresh() {
    setTimeout(() => {
      location.href = '/';
    }, 200);
  }

  doRegister(){
    this.log.debug('doRegister...');

    this.dialog.open(RegisterDialogComponent, {
      width: '560px'
    });

  }

  doForgot(){
    this.log.debug('doForgot...');
  }



}
